Skip to content

[Dataflow Streaming] Reuse ByteStringOutputStream buffers to reduce GC overhead - #36165

Merged
scwhittle merged 6 commits into
apache:masterfrom
arunpandianp:microopt1
Sep 18, 2025
Merged

[Dataflow Streaming] Reuse ByteStringOutputStream buffers to reduce GC overhead#36165
scwhittle merged 6 commits into
apache:masterfrom
arunpandianp:microopt1

Conversation

@arunpandianp

Copy link
Copy Markdown
Contributor

#33578

Also removed an unconditional stream.toByteString() in checkState.

@arunpandianp

Copy link
Copy Markdown
Contributor Author

R: @scwhittle

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @arunpandianp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on optimizing the performance of Dataflow streaming jobs by significantly reducing garbage collection overhead. It achieves this by introducing a robust strategy for reusing ByteStringOutputStream buffers across multiple operations within the WindmillSink and WindmillStateUtil components. This change is crucial for improving the efficiency and stability of high-throughput streaming applications where frequent buffer allocations can lead to performance bottlenecks.

Highlights

  • ByteStringOutputStream Reuse: Implemented reuse of ByteStringOutputStream instances in WindmillSink and WindmillStateUtil to reduce object allocation and garbage collection overhead, particularly in performance-critical encoding paths.
  • Thread-Local Buffer Management: WindmillStateUtil now utilizes a ThreadLocal with SoftReference to manage ByteStringOutputStream instances, enabling efficient per-thread buffer reuse for encoding state keys.
  • Refactored Metadata Encoding: The encodeMetadata method in WindmillSink has been refactored to accept and utilize a reusable ByteStringOutputStream, ensuring the buffer is reset after use via toByteStringAndReset().
  • Removed Guava Preconditions: The dependency on Guava's Preconditions.checkState was removed from WindmillSink.
  • Potential Logical Error in Stream Check: A logical inversion was introduced when replacing checkState(stream.size() == 0, ...) with if (stream.size() == 0) { throw new IllegalStateException(...) } in WindmillSink.WindmillStreamWriter.encode. The original code asserted that the stream must be empty before encoding, throwing an error if it was not. The new code, however, throws an exception if the stream is empty, which is the expected and desired state. This appears to be a bug that could prevent correct operation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@arunpandianp

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a good optimization by reusing ByteStringOutputStream buffers to reduce GC overhead in WindmillSink and WindmillStateUtil. The removal of the unconditional stream.toByteString() in the checkState is also a nice performance improvement.

My review includes a few suggestions to further improve the implementation:

  • In WindmillStateUtil, the new thread-local caching logic can be made more efficient and robust by improving the reset logic and exception handling to prevent cache corruption.
  • In WindmillSink, I've suggested a minor change to make exception handling more consistent and align with best practices.

Overall, these are valuable performance enhancements.

@arunpandianp

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces performance optimizations by reusing ByteStringOutputStream buffers to reduce garbage collection overhead. This is achieved by caching the stream in a ThreadLocal in WindmillStateUtil and by passing a reusable stream to encodeMetadata in WindmillSink. Additionally, an unnecessary call to stream.toByteString() is removed from a precondition check. The changes are well-implemented and should improve performance. I have a couple of suggestions regarding exception handling to improve code clarity and robustness.

@arunpandianp

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a performance optimization by reusing ByteStringOutputStream buffers to reduce garbage collection overhead. The changes are implemented in WindmillSink and WindmillStateUtil. In WindmillSink, a stream is now held as a field and reused for encoding metadata and values. In WindmillStateUtil, a ThreadLocal<SoftReference<...>> is used to cache and reuse a stream for encoding state keys. Additionally, an unconditional call to stream.toByteString() within a checkState has been removed to avoid unnecessary object creation. The changes are well-implemented and robust, ensuring that the reusable streams are reset correctly even in case of exceptions. I have one suggestion to simplify exception handling logic in WindmillStateUtil to reduce code duplication.

@arunpandianp
arunpandianp force-pushed the microopt1 branch 2 times, most recently from a4d902a to a581e34 Compare September 15, 2025 23:50
@codecov

codecov Bot commented Sep 17, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.92%. Comparing base (e8a87a4) to head (2dd4952).
⚠️ Report is 15 commits behind head on master.

Files with missing lines Patch % Lines
...che/beam/runners/dataflow/worker/WindmillSink.java 50.00% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #36165      +/-   ##
============================================
+ Coverage     56.85%   58.92%   +2.06%     
- Complexity     3385    15514   +12129     
============================================
  Files          1220     2846    +1626     
  Lines        185458   277621   +92163     
  Branches       3520    12444    +8924     
============================================
+ Hits         105447   163575   +58128     
- Misses        76681   107600   +30919     
- Partials       3330     6446    +3116     
Flag Coverage Δ
java 64.86% <50.00%> (-5.51%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

throw new RuntimeException(e);
} finally {
if (stream.size() > 0) {
stream.toByteStringAndReset();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add a reset or clear method that is cheap if empty and perhaps remove the size check. could use in the Windmill sink exception handling also

@github-actions github-actions Bot added the java label Sep 17, 2025
@scwhittle

Copy link
Copy Markdown
Contributor

Failures unrelated (sending fix soon for GetData stream one).

@scwhittle
scwhittle merged commit dfeea35 into apache:master Sep 18, 2025
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants